package com.seventh.associationsystem.service;

import com.seventh.associationsystem.bean.Association;
import com.seventh.associationsystem.bean.CreateApplication;
import com.seventh.associationsystem.bean.JoinApplication;
import com.seventh.associationsystem.bean.User;
import com.seventh.associationsystem.repository.CreateApplicationRepository;
import com.seventh.associationsystem.repository.JoinApplicationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class ApplicationService {

    @Autowired
    private CreateApplicationRepository createApplicationRepository;

    @Autowired
    private JoinApplicationRepository joinApplicationRepository;

    @Transactional
    public void addCreateApplication(CreateApplication createApplication) {
        createApplicationRepository.save(createApplication);
    }

    public Page<CreateApplication> getCreateApplicationsPageable(User user, Pageable pageable) {
        return createApplicationRepository.findByUser(user, pageable);
    }

    public Page<CreateApplication> getCreateApplicationsPageable(List<Association> associations, Pageable pageable) {
        return createApplicationRepository.findByAssociationIn(associations, pageable);
    }

    public void updateCreateApplication(CreateApplication createApplication) {
        createApplicationRepository.save(createApplication);
    }

    public void deleteCreateApplication(CreateApplication createApplication) {
        createApplicationRepository.delete(createApplication);
    }

    public CreateApplication getCreateApplication(String uid) {
        return createApplicationRepository.findByUid(uid);
    }

    public void addJoinApplication(JoinApplication joinApplication) {
        joinApplicationRepository.save(joinApplication);
    }
}
